package routermodel

import (
	"context"
	"vpn-management-server/network"
)

// 预处理部分
type beforeHandle func(ctx context.Context, request *network.NetRequest, response *network.NetResponse) bool

// 核心处理部分
type handleFunc func(ctx context.Context, request *network.NetRequest, response *network.NetResponse)

// 收尾处理部分
type afterHandle func(ctx context.Context, request *network.NetRequest, response *network.NetResponse)

type Router struct {
	BeforeHandleSet map[string]beforeHandle // 执行命令前的预备工作
	HandleSet       map[string]handleFunc   // 命令集
	AfterHandleSet  map[string]afterHandle  // 执行命令后的工作，如产生日志存入缓冲队列
}

func NewRouter() *Router {
	return &Router{
		BeforeHandleSet: make(map[string]beforeHandle),
		HandleSet:       make(map[string]handleFunc),
		AfterHandleSet:  make(map[string]afterHandle),
	}
}

func (r *Router) RegisterBeforeHandle(name string, beforeHandle beforeHandle) *Router {
	r.BeforeHandleSet[name] = beforeHandle
	return r
}

func (r *Router) RegisterHandle(name string, handle handleFunc) *Router {
	r.HandleSet[name] = handle
	return r
}

func (r *Router) RegisterAfterHandle(name string, afterHandle afterHandle) *Router {
	r.AfterHandleSet[name] = afterHandle
	return r
}

func (r *Router) RegisterHandleSet(name string, beforeHandle beforeHandle, handle handleFunc, afterHandle afterHandle) {
	r.BeforeHandleSet[name] = beforeHandle
	r.HandleSet[name] = handle
	r.AfterHandleSet[name] = afterHandle
}
